<template>
  <div style="background-color: #f0f2f5">
    <SkuDetail ref="SkuDetail" class="array" />
    <el-card class="kuanged">
      <el-button type="primary" size="default" @click="adds">新增</el-button>
      <el-table :data="tableData" style="width: 100%" max-height="1000" border>
        <el-table-column fixed type="index" width="50" label="序号">
        </el-table-column>
        <el-table-column
          width="150"
          label="商品名称"
          prop="name"
          align="center"
          class="names"
        >
          <template slot-scope="scope">
            <div>{{ scope.row.name }}</div>
          </template>
        </el-table-column>
        <el-table-column label="展示状态" width="150">
          <template slot-scope="scope">
            <div>
              是否展示：<el-switch
                v-model="scope.row.showStatus"
                active-color="#409EFF"
                inactive-color="#DCEFE6"
                :active-value="1"
                :inactive-value="0"
                @change="newest(scope.row)"
              >
              </el-switch>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="province"
          label="logo"
          width="100"
          align="center"
        >
          <template slot-scope="scope">
            <img
              style="width: 100px; height: 100px"
              :src="scope.row.logo"
              alt=""
            />
          </template>
        </el-table-column>

        <el-table-column
          prop="province"
          label="专区大图"
          width="100"
          align="center"
        >
          <template slot-scope="scope">
            <img
              style="width: 100px; height: 100px"
              :src="scope.row.bigPic"
              alt=""
            />
          </template>
        </el-table-column>
        <el-table-column
          width="150"
          label="品牌故事"
          prop="brandName"
          align="center"
        >
          <template slot-scope="scope">
            <div>{{ scope.row.brandStory }}</div>
          </template>
        </el-table-column>
        <el-table-column
          width="150"
          label="创建时间"
          prop="createTime"
          align="center"
        >
        </el-table-column>

        <el-table-column
          width="150"
          label="排序"
          prop="sort"
          align="center"
        ></el-table-column>
        <el-table-column
          width="110"
          label="产品评论数量"
          prop="productCommentCount"
          align="center"
        ></el-table-column>
        <el-table-column
          width="110"
          label="产品数量"
          prop="productCount"
          align="center"
        ></el-table-column>
        <el-table-column width="150" label="是否品牌制造商" align="center">
          <template slot-scope="scope">
            <div v-if="scope.row.factoryStatus == 1">是</div>
            <div v-else>不是</div>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="120">
          <template slot-scope="scope">
            <el-button
              @click="openDetail(scope.row)"
              type="text"
              size="small"
              style="color: red !important"
            >
              编辑
            </el-button>
            <el-button
              @click="deleteRow(scope.row.id)"
              type="text"
              size="small"
              style="color: red !important"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import SkuDetail from './components/SkuDetail.vue'
import product from '@/api/product.js'
import mixin from '@/mixins'
export default {
  mixins: [mixin],
  components: {
    SkuDetail,
  },
  data() {
    return {
      info: '',
      tableData: [],
      value: '',
    }
  },
  created() {},
  mounted() {
    this.findBrandByPageApi()
  },
  methods: {
    // findBrandByPageApi() {
    //   product.findBrandByPageApi(10, 1).then(res => {
    //     console.log(res)
    //   })
    // },
    findBrandByPageApi() {
      setTimeout(() => {
        product
          .findBrandByPageApi(
            this.page.currentPage,
            this.page.pageSize,
            this.from,
            {
              showStatus: 1,
            }
          )
          .then(res => {
            if (res.success) {
              this.tableData = res.data.rows
              this.page.total = res.data.total
            } else {
              this.$message.error('请求失败')
            }
            if (this.tableData.newStatus == 0) {
              this.value = false
            } else {
              this.value = true
            }
            console.log(this.tableData, 'this.tableData')
          })
      })
    },
    newest(row) {
      console.log(row.showStatus, 'row')
      // var ermpStatus = row.showStatus === 1 ? 0 : 1 //如果失败了  0就是1  1就是0
      product
        .switchShowStatusApi({
          id: row.id,
          status: row.showStatus,
        })
        .then(res => {
          console.log(res, 'res')
          if (res.success == true) {
            this.$message.success('修改状态成功')
          } else {
            this.$message.error('修改状态失败，请联系管理员')
            // row.showStatus = ermpStatus
          }
        })
    },
    //删除商品
    deleteRow(row) {
      console.log(row, '删除')
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        product.delBrandApi(row).then(res => {
          if (res.success) {
            this.$message.success('删除成功')
            this.findBrandByPageApi()
          } else {
            console.log('1111')
            this.$message.error(res.message)
          }
        })
      })
    },
    //编辑
    openDetail(row) {
      // console.log(row, 'row')
      // this.info = row
      this.$refs.SkuDetail.open(row)
    },
    //新增
    adds() {
      // this.info = ''
      this.$refs.SkuDetail.open()
    },
  },
}
</script>

<style>
.kuanged {
  margin: 2%;
  margin-top: 20px;
  background-color: #fff;
}
</style>